package bstsetcode;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/14 8:47
 */
public class Array {


    private int[] data;
    private int size;

    /**
     *
     * @param capacity  数组容量
     */
    public Array(int capacity) {
        data= new int[capacity];
        size = 0;
    }

    public Array() {
        this(10);
    }

    /**
     * 数组中实际元素的个数
     * @return 实际个数
     */
    public int getSize(){
        return size;
    }

    public int getCapacity(){
        return data.length;
    }


    public boolean isEmpty(){
        return size == 0;
    }

    public void addLast(int e){
//        if(size == data.length){
//            throw new IllegalArgumentException("AddLast failed. Array is full.");
//        }
//        data[size] = e;
//        size++;
        add(size,e);
    }

    public void addFirst(int e){
        add(0,e);

    }

    public void add(int index, int e) {
        if(size == data.length) {
            throw new IllegalArgumentException("Add failed. Array is full.");
        }
        if(index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
        }
        for (int i = size-1; i >= index; i--) {
            data[i+1]=data[i];
        }
        data[index] = e;
        size++;
    }


    public int getElementByIndex(int index){
        if(index < 0 || index >=size){
            throw  new IllegalArgumentException("ArrayIndexOutOfBounds ");
        }
        return  data[index];
    }

    /**
     * 修改index索引位置的元素e
     */
    public void setElement(int index,int e){
        if(index < 0 || index >=size){
            throw  new IllegalArgumentException("ArrayIndexOutOfBounds ");
        }
        data[index] = e;
    }

    public boolean contains(int e){
        for (int i = 0; i < size; i++) {
            if(data[i] == e){
                return true;
            }
        }
        return false;
    }

    /**
     * 找到某个元素的索引
     * @param e  元素
     * @return 索引  不存在就返回-1
     */
    public int findIndex(int e){

        for (int i = 0; i < size; i++) {
            if(data[i] == e){
                return i;
            }
        }
        return -1;
    }

    /**
     * 删除指定的索引位置的元素值
     * @return
     */

    public int remove(int index){
        if(index < 0 || index >=size){
            throw  new IllegalArgumentException("Remove failed. Index is not legal ");
        }
        int res = data[index];
        for (int i = index+1; i < size; i++) {
            data[i-1]=data[i];
        }
        size--;
        return res;
    }

    public int removeFirst(){
       return remove(0);
    }


    public int removeLast(){
        return remove(size-1);
    }

    public void removeElement(int e){
        int index  =findIndex(e);
        if(index!=-1){
            remove(index);
        }
    }








    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d, capacity = %d\n",size,data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size-1){
                res.append(", ");
            }
        }
        res.append("]");
        return res.toString();
    }
}
